<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\Commentstatus;
use yii\grid\Column;

/* @var $this yii\web\View */
/* @var $searchModel common\models\CommentSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '评论管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<div class="comment-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            // ['class' => 'yii\grid\SerialColumn'],

            //'id',
            [
                'attribute' => 'id',
                'contentOptions' => ['width' => '30px'],
            ],
            //'content:ntext',
            [
                'attribute' => 'content',
                'value' => 'beginning',
//             'value'=>function($model)
//             		{
//             			$tmpStr=strip_tags($model->content);
//             			$tmpLen=mb_strlen($tmpStr);

//             			return mb_substr($tmpStr,0,20,'utf-8').(($tmpLen>20)?'...':'');
//             		}
            ],
            //'userid',
            [
                'attribute' => 'user.username',
                'label' => '作者',
                'value' => 'user.wxname',
            ],
            //'status',
            [
                'attribute' => 'status',
                'value' => 'status0.name',
                'filter' => Commentstatus::find()
                    ->select(['name', 'id'])
                    ->orderBy('position')
                    ->indexBy('id')
                    ->column(),
                'contentOptions' =>
                    function ($model) {
                        return ($model->status == 1) ? ['class' => 'bg-danger'] : [];
                    }
            ],
            // 'create_time:datetime',
            [
                'attribute' => 'create_time',
                'format' => ['date', 'php:m-d H:i'],
            ],

            // 'email:email',
            // 'url:url',
            // 'post_id',
            'post.title',

            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update} {delete} {approve}{send}',
                'buttons' =>
                    [
                        'approve' => function ($url, $model, $key) {
                            $options = [
                                'title' => Yii::t('yii', '审核'),
                                'aria-label' => Yii::t('yii', '审核'),
                                'data-confirm' => Yii::t('yii', '你确定通过这条评论吗？'),
                                'data-method' => 'post',
                                'data-pjax' => '0',
                            ];
                            return Html::a('<span class="glyphicon glyphicon-check"></span>', $url, $options);

                        },
                        'send' => function ($url, $model, $key) {
                            $options = [
                                'title' => Yii::t('yii', '回复'),
                                'aria-label' => Yii::t('yii', '回复'),
//                            'data-method'=>'post',
                                'data-pjax' => '0',
                                'onclick' => "test(this)",
                                'data-toggle' => "modal",
                                'data-target' => "#myModal",
                                'class' => "glyphicon glyphicon-user",
                                'id' => $key,
                                'style'=>'margin:10px',
                            ];
                            return Html::button('', $options);
                            // return Html::a('<span class="glyphicon glyphicon-check"></span>',$url,$options);

                        },
                    ],


            ],

        ],
    ]); ?>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">回复消息</h4>
            </div>
            <input type="hidden" class="content123" name="id">
            <input type="text" class="form-control" id="bodycon" placeholder="请输入内容">
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="a()">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    var id;
    function test(element) {
        id = element.id
    }
    function a() {
        var text = document.getElementById("bodycon").value;
        if (text.length == 0) {
            alert("内容不允许为空")
            return false;
        }
        $.ajax({

            url: "index.php?r=user/send",
            data: {id:id ,text:text},
            type: "get",
            dataType : 'json',
            success: function (msg) {
                if (msg.status ==200){
                    alert(msg.msg)
                    $('#myModal').modal('hide')
                    window.location.reload()
                }else {
                    alert(msg.msg)
                }

            }


        });

    }
</script>
